<template>
  <el-dialog title="收货确认" width="50%" :visible.sync="visible">
    <el-form ref="form" :model="form" class="dialog-form" label-position="right" label-width="120px">
      <el-form-item label="部分退货" prop="params.type">
        <el-checkbox v-model="form.params.type" true-label="1" false-label="0" />
      </el-form-item>
      <el-form-item label="回执单" prop="params.receipt" :rules="self.page.rules.def">
        <form-upload-image v-model="form.params.receipt" size="mini" :remote="self.upload.img" />
      </el-form-item>
      <!-- <el-form-item label="参考扣点" prop="refDeductQuantity">
        <label><span class="money">{{ refDeductQuantity.toFixed(3) }}</span> 吨</label>
      </el-form-item> -->
      <el-form-item label="扣点" prop="params.deduct" :rules="self.page.rules.num">
        <label><span class="money">{{ form.params.deduct }}</span> %</label>
      </el-form-item>
      <el-form-item label="扣点重量" prop="params.deductQuantity" :rules="self.page.rules.num">
        <label><span class="money">{{ form.params.deductQuantity.toFixed(3) }}</span> 吨</label>
      </el-form-item>
      <!-- <el-form-item label="水分" prop="params.waterQuantity" :rules="self.page.rules.num">
        <el-input-number v-model="form.params.waterQuantity" controls-position="right" :precision="3" :min="0" @change="cal" />
      </el-form-item>
      <el-form-item label="杂质" prop="params.impurityQuantity" :rules="self.page.rules.num">
        <el-input-number v-model="form.params.impurityQuantity" controls-position="right" :precision="3" :min="0" @change="cal" />
      </el-form-item>
      <el-form-item label="杂纸" prop="params.discardQuantity" :rules="self.page.rules.num">
        <el-input-number v-model="form.params.discardQuantity" controls-position="right" :precision="3" :min="0" @change="cal" />
      </el-form-item>
      <el-form-item label="其他" prop="params.otherQuantity" :rules="self.page.rules.num">
        <el-input-number v-model="form.params.otherQuantity" controls-position="right" :precision="3" :min="0" @change="cal" />
      </el-form-item> -->
      <el-form-item label="描述" prop="message">
        <el-input v-model="form.message" type="textarea" :rows="2" placeholder="请输入描述" />
      </el-form-item>
      <div>
        <el-divider content-position="left">上传凭证 <el-button icon="el-icon-plus" type="text" @click="plus" /></el-divider>
        <el-form-item
          v-for="(item, index) in form.params.certificates"
          :key="item.key"
          :label="'凭证' + (index+1)"
          :prop="'params.certificates.' + index + '.picture'"
        >
          <form-upload-image v-model="item.picture" size="mini" :remote="self.upload.img" />
          <el-button icon="el-icon-delete" type="text" @click="remove(item)" />
        </el-form-item>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">取 消</el-button>
      <el-button size="mini" type="primary" @click="submit">提交</el-button>
    </div>
  </el-dialog>
</template>
<script>
export default {
  name: 'PurEceiving',
  props: {
    value: {
      type: String,
      default: ''
    },
    self: {
      type: Object,
      default: () => { return {} }
    }
  },
  data() {
    return {
      visible: false,
      form: {
        id: null,
        message: '',
        params: {
          receipt: '',
          deduct: 0,
          deductQuantity: 0,
          waterQuantity: 0,
          impurityQuantity: 0,
          otherQuantity: 0,
          discardQuantity: 0,
          certificates: [],
          type: 0
        }
      }
    }
  },
  created() {

  },
  methods: {
    show(deductQuantity, deduct) {
      this.visible = true
      this.form.params.deductQuantity = deductQuantity
      this.form.params.deduct = deduct
    },
    submit() {
      const that = this
      that.form.id = that.value
      const certificates = []
      // console.log(this.form.params.certificates)
      this.form.params.certificates.forEach((item) => {
        certificates.push(item.picture)
      })
      this.form.params.certificates = certificates
      console.log(that.form)
      that.$refs['form'].validate((valid) => {
        if (!valid) {
          return
        }
        if (!that.valid()) {
          this.$message.error('请认真添加扣点项')
          return
        }
        const loading = that.$loading()
        const url = that.self.page.router.remoteUrl + '/eceiving'
        that.self.service.post(url, this.form).then(res => {
          loading.close()
          if (res.code !== 200) {
            that.$message.error(res.message)
            return
          }
          that.$message.success('操作成功!')
          that.self.reload(true)
        }).catch((e) => {
          loading.close()
        })
      })
    },
    valid() {
      // 扣点 = 水分+杂质+其他
      // const deductQuantity = this.form.params.waterQuantity + this.form.params.impurityQuantity + this.form.params.otherQuantity + this.form.params.discardQuantity
      // if (deductQuantity !== this.form.params.deductQuantity) {
      //   return false
      // }
      return true
    },
    cal() {
      // this.form.params.deductQuantity = this.form.params.waterQuantity + this.form.params.impurityQuantity + this.form.params.otherQuantity + this.form.params.discardQuantity
    },
    plus() {
      // this.form.params.certificates.push({ picture: '', key: Date.now() })
    },
    remove(item) {
      var index = this.form.params.certificates.indexOf(item)
      if (index !== -1) {
        this.form.params.certificates.splice(index, 1)
      }
    }

  }
}
</script>
<style scoped>
.upload-image{
  float: left;
  margin-right: 10px;
}
</style>
